<?php

function handle_room_booking_form() {
	if (isset($_POST['csrf_token'])) {
		if (!isset($_POST['room_booking_nonce']) || !wp_verify_nonce($_POST['room_booking_nonce'], 'process_room_booking')) {
			die('Security check failed');
		}

		$csrf_token = sanitize_textarea_field($_POST['csrf_token']);
		$csrf_token = trim($csrf_token);
		$result = process_booking_json_template($csrf_token);
        set_transient('room_booking_result', $result, 360);
		wp_redirect(home_url('/process-room-bookings/'));
		exit;
	}
}

add_action('init', 'handle_room_booking_form');


function handle_room_bookings_json() {
	if (isset($_POST['booking_json'])) {
		if (!isset($_POST['room_booking_json_nonce']) || !wp_verify_nonce($_POST['room_booking_json_nonce'], 'process_room_booking_json')) {
			die('Security check failed');
		}

		$booking_json = sanitize_textarea_field($_POST['booking_json']);
		file_put_contents(__DIR__ . '/files/'.time().'.json', $booking_json);
		$booking_json = trim($booking_json);
		//submit booking json to the server
		wp_redirect(home_url('/process-room-bookings/'));
		exit;
	}
}

add_action('init', 'handle_room_bookings_json');